package com.ch999.detect.View.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ch999.detect.R;
import com.ch999.detect.constant.SpType;
import com.ch999.detect.mode.bean.GoodsBjgz;
import com.ch999.detect.utils.SharePrefUtil;

import java.util.List;
import java.util.Map;

import static com.ch999.detect.constant.SpType.KEY_COUNT;


/**
 * Created by Administrator on 2016/12/13 0013.
 */

public class FacationAdatper extends BaseAdapter {
    public Context mContext;
    public List<GoodsBjgz.AttrBean> data;
    private RecyclerView mRecyclerView;

    public int mBaseSize = 0;

    public int showItemNumb;//已经显示的条目

    Map<String, Integer> mFacationImageList;

    /**
     * 基本功能的条目数
     *
     * @param context
     * @param data
     * @param baseSize
     */
    public FacationAdatper(Context context, List<GoodsBjgz.AttrBean> data, int baseSize, Map<String, Integer> facationImageList) {
        this.mContext = context;
        this.data = data;
        this.mBaseSize = baseSize;
        this.mFacationImageList = facationImageList;
    }

    public interface OnClickItemListener {
        void onClickItemListener(int posation, GoodsBjgz.AttrBean attrBean);
    }

    OnClickItemListener onClickItemListener;

    public void setOnClickItemListener(OnClickItemListener onClickItemListener) {
        this.onClickItemListener = onClickItemListener;
    }


    @Override
    public int getCount() {

        int count = 0;

        if (data == null) {
            return 0;
        }


        int anInt = SharePrefUtil.getInstance(mContext).getInt(SpType.KEY_POSATION);

        if (anInt >= -1) {

            if (anInt < showItemNumb) {
                count = showItemNumb;
            } else {
                count = showItemNumb = anInt + 1;
            }

        }

        if (count >= data.size()) {
            return data.size();
        }

        return count;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int position, View view, ViewGroup viewGroup) {

        View inflate = LayoutInflater.from(mContext).inflate(R.layout.item_facation, null, false);


        FacationHolder holder = new FacationHolder(inflate);


        final GoodsBjgz.AttrBean attrBean = data.get(position);
        String tpname = attrBean.getTpname();
        holder.tvFacationName.setText(tpname);

        int itemPosation = attrBean.getItemPosation();

        //非基本功能不展现
        if (position >= mBaseSize) {

            GoodsBjgz.AttrBean.ValuesBean valuesBean = attrBean.getValues().get(itemPosation);

            holder.tvPname.setVisibility(View.VISIBLE);

            if (attrBean.isDetect()) {
                holder.tvPname.setVisibility(View.VISIBLE);
            } else {
                holder.tvPname.setVisibility(View.INVISIBLE);
            }

            String name = valuesBean.getName();

            holder.tvPname.setText(name);

            holder.imgIcon.setBackgroundResource(R.drawable.reset);

            holder.imgNormal.setVisibility(View.VISIBLE);
            holder.imgNormal.setBackgroundResource(R.mipmap.right);

            isSaveAccountFalse(tpname, name);
        } else {

            if (attrBean.isDetect()) {
                holder.imgNormal.setVisibility(View.VISIBLE);
            } else {
                holder.imgNormal.setVisibility(View.INVISIBLE);
            }

            Integer integer = mFacationImageList.get(attrBean.getTpname());

            holder.tvPname.setVisibility(View.GONE);
            if (integer != null) {

                holder.imgIcon.setBackgroundResource(integer.intValue());
            }

            if (itemPosation == 0) {
                holder.imgNormal.setBackgroundResource(R.mipmap.pass_new);
            } else {
                holder.imgNormal.setBackgroundResource(R.mipmap.unnuomal);
            }
        }


        holder.llRootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onClickItemListener != null) {
                    onClickItemListener.onClickItemListener(position, attrBean);
                }
            }
        });

        return inflate;
    }


    /**
     * 检测账号是否被删除
     * @param tpname
     * @param string
     */
    public void isSaveAccountFalse(String tpname,String string) {

        if (!TextUtils.isEmpty(tpname) && tpname.equals("手机账户")  ) {

            if (!TextUtils.isEmpty(string) && string.equals("账户无法解除（不回收）")) {
                SharePrefUtil.getInstance(mContext).putBoolean(KEY_COUNT, false);
            } else {
                SharePrefUtil.getInstance(mContext).putBoolean(KEY_COUNT, true);
            }
        }
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    public void setData(List<GoodsBjgz.AttrBean> data) {
        if (data == null) {
            return;
        }

        this.data = data;
        notifyDataSetChanged();
    }

    public void setData(List<GoodsBjgz.AttrBean> data, Map<String, Integer> facationImageList) {
        if (data == null) {
            return;
        }


        this.data = data;
        mFacationImageList = facationImageList;
        mBaseSize = mFacationImageList.size();
        notifyDataSetChanged();
    }


    public static class FacationHolder {

        ImageView imgIcon;
        TextView tvFacationName;
        ImageView imgNormal;
        TextView tvPname;

        LinearLayout llRootView;

        public FacationHolder(View itemView) {
           imgIcon = (ImageView)itemView.findViewById(R.id.img_icon);
             tvFacationName = (TextView)itemView.findViewById(R.id.tv_facation_name);
             imgNormal = (ImageView)itemView.findViewById(R.id.img_normal);
             tvPname = (TextView)itemView.findViewById(R.id.tv_p_name);

             llRootView = (LinearLayout)itemView.findViewById(R.id.ll_root_view);
        }


    }


}
